﻿using System;
using UnityEngine;
using System.Collections.Generic;
using DG.Tweening;
using DevelopTools.UI;
namespace DevelopTools
{
    public class ItemObject : MonoBehaviour
    {
        [Header("显示的UI面板")]
        public List<UIPanel> UIList;
        [Header("播放的特效")]
        public List<ParticleItem> ParticleList;

        private void Start()
        {
            Init();
        }
        /// <summary>
        /// 初始化：UI和粒子
        /// </summary>
        private void Init()
        {
            //初始化UI
            UIManager.Instance.AddPanel(UIList);
            //初始化粒子
            for (int i = 0; i < ParticleList.Count; i++)
            {
                ParticleList[i].transform.gameObject.SetActive(false);
            }
        }
        //private Transform hitTrans;
        /// <summary>
        /// 显示UI：
        /// </summary>
        /// <param name="hitTrans">显示UI的位置</param>
        public void ShowUI(Vector3 hitPoint, Vector3 hitNormal)
        {
            //显示UI

            UIManager.Instance.ShowUIPanel(UIList);
            //for (int i = 0; i < UIList.Count; i++)
            //{
            //    //UIList[i].SetActive(true);
            //    UIList[i].transform.DOBlendableScaleBy(Vector3.zero, 0.8f);
            //}

            //播放

            PlayParticle(hitPoint, hitNormal);
        }
        /// <summary>
        /// 关闭UI
        /// </summary>
        public void HideUI()
        {
            //隐藏UI
            UIManager.Instance.HideUIPanel(UIList);
            //for (int i = 0; i < UIList.Count; i++)
            //{
            //    //UIList[i].SetActive(false);
            //}
        }
        /// <summary>
        ///  播放当前的特效
        /// </summary>
        /// <param name="hitPoint">检测到的位置点</param>
        /// <param name="hitNormal">检测到的位置点法线方向</param>
        private void PlayParticle(Vector3 hitPoint, Vector3 hitNormal)
        {
            for (int i = 0; i < ParticleList.Count; i++)
            {
                ParticleList[i].transform.position = new Vector3(
                    hitPoint.x, hitPoint.y, hitPoint.z);
                ParticleList[i].transform.LookAt(hitPoint - hitNormal);
                //将特效放置在摄像机前方，世界坐标与视口坐标转换再转回
                Vector3 temp = Camera.main.WorldToViewportPoint(hitPoint);
                ParticleList[i].transform.position = Camera.main.ViewportToWorldPoint(
                    new Vector3(temp.x, temp.y, temp.z - 1f));

                ParticleList[i].transform.gameObject.SetActive(true);
                ParticleList[i].Play();
            }
        }

    }
}

